package zozopo.comm;

import java.util.regex.Pattern;

public class Validate {
	
	//是否是数字
	/** 
	 * Method isNumber
	 * @param String
	 * @return boolean
	 */
	public final static boolean isNumber(String num){
		if(num == null || num.trim().equals("")){return false;}
		byte[] numBytes = num.getBytes();
		
		for(int i =0 ;i<numBytes.length;i++){
			if(48<=numBytes[i] && 57>=numBytes[i]){
			}else{
				return false;
			}
		}
		return true;
	}
	
	//以Double为数值上限的 数字验证.
	/** 
	 * Method isDouble
	 * @param String
	 * @return boolean
	 */
	public final static boolean isDouble(String num){
		if(num == null){return false;}
		if(num.startsWith(".") || num.endsWith(".")){
			return false;
		}
		try{
			Double.parseDouble(num);
		}catch(NumberFormatException e){
			return false;
		}
		return true;
	}
	
	//以Integer为数值上限的 正整数字验证.
	/** 
	 * Method isInteger
	 * @param String
	 * @return boolean
	 */
	public final static boolean isInteger(String num){
		if(num == null){return false;}
		try{
			Integer.parseInt(num);
		}catch(NumberFormatException e){
			return false;
		}
		return true;
	}
	
	//是否存在字符解析时的特殊用到的字符.
	/** 
	 * Method isSpecialCharacters
	 * @param String
	 * @return boolean
	 */
	public final static boolean isSpecialCharacters(String content){
		if(content == null){return false;}
		String containChar =";\"'\\`|$";
		if(isContain(content, containChar.getBytes())){
			return true;
		}
		return false;
	}
	
	/** 
	 * Method isSpecialCharacters
	 * @param String
	 * @return boolean
	 * @see 是否是正确的URL连接格式 未完成
	 */
	public final static boolean isURL(String url){
		//if(url == null){return false;}
		//String regexForFontAtt ="/^http:\/\/.{0,93}/";
		//if(isContain(content, containChar.getBytes())){
		//	return true;
		//}
		return false;
	}
	
	//是否是E-Mail格式
	/** 
	 * Method isEmail
	 * @param String
	 * @return boolean
	 */
	public final static boolean isEmail(String email){
		if(email == null){return false;}
		if(isSpecialCharacters(email)){
			 return false;
		 }
		
		if(isContain(email, "@.")){
			return false;
		}
		
		 if(!isCenter(email, "@") || !isCenter(email, ".")){
			 return false;
		 }
		return true;
	}
	
	/** 
	 * Method isContain
	 * String 是否包含这些 byte[]字符
	 * @param String
	 * @param byte[] 
	 * @return boolean
	 */
	public final static boolean isContain(String content,byte[] chars){
		for(int i=0;i<chars.length;i++){
			char c = (char)chars[i];
			if(content.contains(String.valueOf(c))){
				return true;
			}
		}
		return false;
	}
	
	/** 
	 * Method isContain
	 * 是否包含这些 字段
	 * @param String
	 * @param String
	 * @return boolean
	 */
	public final static boolean isContain(String content,String containString){
		if(content.contains(containString)){
				return true;
			}
		return false;
	}
	
	/** 
	 * Method isCenter
	 * 是否在中间而且是唯一的 并且要存在一个
	 * @param String
	 * @param String
	 * @return boolean
	 */
	public final static boolean isCenter(String content,String containChar){
		 if(content.indexOf(containChar) != content.lastIndexOf(containChar) ||
			content.startsWith(containChar) || content.endsWith(containChar)
			|| content.indexOf(containChar) < 0
		 ) {
			 return false;
		 }else{
			 return true;
		 }
	}
	/** 
	 * Method insertHtmlFilter
	 * 过滤非法HTML
	 * @param String
	 * @param String
	 * @return String
	 */
	public final static String htmlLabelFilter(String inputString){
		if(inputString == null){
			return "";
		}
		String htmlStr = inputString; // 含html标签的字符串
		String textStr = "";
		java.util.regex.Pattern p_script;
		java.util.regex.Matcher m_script;
		java.util.regex.Pattern p_style;
		java.util.regex.Matcher m_style;
		java.util.regex.Pattern p_html;
		java.util.regex.Matcher m_html;
		
		java.util.regex.Pattern p_javascript;
		java.util.regex.Matcher m_javascript;
		
		java.util.regex.Pattern p_attribute;
		java.util.regex.Matcher m_attribute;
		 
		String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; // 定义script的正则表达式{或<script>]*?>[\s\S]*?<\/script>
		   // }
		   String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; // 定义style的正则表达式{或<style>]*?>[\s\S]*?<\/style>
		   // }
		   String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
		   
		   String regEx_javascript ="javascript:(.+?)['\"\\s)]";
		   
		   String regEx_attribute = "onload=(['\"]?)(.+?)(['\"])";
		   
			   //*= *[\s\S]*script. *: s*(href|src)s*=s*("s*(javascript|vbscript):[^"]+"|'s*(javascript|vbscript):[^']+'|(javascript|vbscript):[^s]+)s*(?=>)
		   p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
		   m_script = p_script.matcher(htmlStr);
		   htmlStr = m_script.replaceAll(""); // 过滤script标签

		   p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
		   m_style = p_style.matcher(htmlStr);
		   htmlStr = m_style.replaceAll(""); // 过滤style标签
		   
		   /*
		   p_javascript = Pattern.compile(regEx_javascript, Pattern.CASE_INSENSITIVE);
		   m_javascript = p_javascript.matcher(htmlStr);
		   htmlStr = m_javascript.replaceAll("");
		   
		   
		   /*
		   p_attribute = Pattern.compile(regEx_attribute, Pattern.CASE_INSENSITIVE);
		   m_attribute = p_attribute.matcher(htmlStr);
		   htmlStr = m_attribute.replaceAll("$1");
		   
		   String regEx_td = "<[\\s]*?td[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?td[\\s]*?>"; 
		   java.util.regex.Pattern p_td;
		   java.util.regex.Matcher m_td;
		   p_td = Pattern.compile(regEx_td, Pattern.CASE_INSENSITIVE);
		   m_td = p_td.matcher(htmlStr);
		   htmlStr = m_td.replaceAll("");
		   
		   String regEx_DIV = "<[\\s]*?table[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?table[\\s]*?>"; 
		   java.util.regex.Pattern p_DIV;
		   java.util.regex.Matcher m_DIV;
		   p_DIV = Pattern.compile(regEx_DIV, Pattern.CASE_INSENSITIVE);
		   m_DIV = p_td.matcher(htmlStr);
		   htmlStr = m_DIV.replaceAll("");
		   
		  /* p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
		   m_html = p_html.matcher(htmlStr);
		   htmlStr = m_html.replaceAll(""); // 过滤html标签
			*/ 
		   
		   textStr = htmlStr;
		
		return textStr;
		
	}
	
	
	/** 
	 * Method insertHtmlFilter
	 * 过滤HTML
	 * @param String
	 * @param String
	 * @return String
	 */
	public final static String htmlFilter(String content){
		//String content = content; 
	    content=content.replaceAll("</?[^>]+>","");   //剔出了<html>的标签
	    content=content.replace("&nbsp;","");
	    content=content.replace(".","");
	    content=content.replace("\"","‘");
	    content=content.replace("'","‘");
	    content=content.replaceAll("\\s*|\t|\r|\n","");//
		
		return content;
		
	}
	
}
